/**
 * 解析统一业务字符串存储到pinia缓存
 */
import { decryptUrl, Base64 } from '../utils/Encrypt'
// import { mapMutations } from "vuex";
import store from '@/store'
import zzjg from '@/api/zzjg'
import { ElMessage } from 'element-ui'
export const useJxtyrjnr = () => {
  // 获取解析出的统一业务传递内容
  const getJxtyrjnr = fn => {
    const tempObj = decryptUrl()
    const getUrl = () => {
      let ajxx = {}
      let yhxx = {}
      if (!tempObj) {
        return { ajxx, yhxx }
      }
      const str = tempObj
      // 判断跳入方式
      if (str.includes('ajxx=')) {
        // 个案进入
        const arr = str.split('&')[0].split('ajxx=')
        ajxx = JSON.parse(decodeURIComponent(Base64.decode(arr[1])))
        const arr1 = str.split('&')[1].split('yhxx=')
        yhxx = JSON.parse(decodeURIComponent(Base64.decode(arr1[1])))
      } else {
        // 其他进入
        const strs = str.split('&')
        for (let i = 0; i < strs.length; i++) {
          ajxx[strs[i].split('=')[0]] = unescape(strs[i].split('=')[1])
        }
      }
      return { ajxx, yhxx }
    }
    const edlObj = getUrl()
    const params = {
      dwbm: edlObj.yhxx.dlrdwbm || edlObj.ajxx.dwbm,
      rybm: edlObj.yhxx.rybm || edlObj.ajxx.rybm
    }
    store.commit('SET_PERSONINFO', edlObj)
    zzjg
      .getLogin(params)
      .then(() => {
        fn()
      })
      .catch(err => {
        !err.isError && ElMessage({ type: 'warning', message: err.message })
      })
  }
  return {
    getJxtyrjnr
  }
}
